import React from 'react'
import { Table, Modal } from 'antd'
import axios from 'axios'
import { checkresStatus } from 'utils'

export default function Log(props) {
  const { current, record } = props
  const [data, setData] = React.useState([])
  const [pageIdx, setPageIdx] = React.useState(1)
  const [pageSize, setPageSize] = React.useState(10)
  const [total, setTotal] = React.useState(0)
  const [loading, setLoading] = React.useState(false)

  const columns = [
    {
      title: '请求开始时间',
      dataIndex: 'restStartTime',
      key: 'restStartTime',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: val => (val == 1 ? '正常' : '失败'),
    },
    {
      title: '请求结束时间',
      dataIndex: 'restEndTime',
      key: 'restEndTime',
    },
    {
      title: '返回报文',
      dataIndex: 'response',
      key: 'response',
    },
    {
      title: '耗时(毫秒)',
      dataIndex: 'duration',
      key: 'duration',
    },
    {
      title: '请求报文',
      dataIndex: 'request',
      key: 'request',
      render: val => (
        <a
          onClick={() =>
            Modal.info({
              title: '备注',
              content: val,
            })
          }
        >
          查看请求报文详情
        </a>
      ),
    },
    {
      title: '错误信息',
      dataIndex: 'errMsg',
      key: 'errMsg',
    },
  ]

  React.useEffect(() => {
    setPageIdx(1)
  }, [current])

  React.useEffect(() => {
    let type = null
    switch (current) {
      case 0:
        {
          type = 50
        }
        break
      case 1:
        {
          type = 51
        }
        break
      case 2:
        {
          type = 52
        }
        break
      case 3:
        {
          type = 53
        }
        break
    }
    if (!type || !record.vin) return
    setLoading(true)
    axios
      .get('/restlog/queryRestLog', {
        params: {
          pageIdx: pageIdx,
          pageSize: pageSize,
          type: type,
          recordId: record.vin,
        },
      })
      .then(res => {
        setLoading(false)
        if (!checkresStatus(res)) return
        setData(res.data.data.records)
        setTotal(res.data.data.total)
      })
  }, [current, pageIdx, pageSize, record])

  return (
    <Table
      columns={columns}
      dataSource={data}
      loading={loading}
      rowKey="id"
      pagination={{
        current: pageIdx,
        pageSize: pageSize,
        total: total,
        onChange: (page, pageSize) => {
          setPageIdx(page)
          setPageSize(pageSize)
        },
      }}
    ></Table>
  )
}
